import {
  getAccommodation,
  getAccommodationByHot,
  getReservation,
  getSaveAccommodation,
  likeAccommodation,
  releaseAccommodation,
  reservation,
  saveAccommodation
} from "@/service/main/Accommodation/Accommodation"
import type {
  IReleaseAccommodation,
  IReservation
} from "@/service/main/Accommodation/AccommodationParams"
import { defineStore } from "pinia"

interface IAccommodationState {
  accommodations: IAccommodations
  accommodationsByHot: IAccommodations
  saveAccommodations: IAccommodations
  reservationAccommodations: IAccommodations
}
interface IAccommodations {
  accommodationLists: any[]
  accommodationTotal: number
}
const useAccommodationStore = defineStore("community", {
  state: (): IAccommodationState => ({
    accommodations: {
      accommodationLists: [],
      accommodationTotal: 0
    },
    accommodationsByHot: {
      accommodationLists: [],
      accommodationTotal: 0
    },
    saveAccommodations: {
      accommodationLists: [],
      accommodationTotal: 0
    },
    reservationAccommodations: {
      accommodationLists: [],
      accommodationTotal: 0
    }
  }),
  actions: {
    async releaseAccommodation(data: IReleaseAccommodation) {
      const result = await releaseAccommodation(data)
      if (result.data.code == 200) {
        return "ok"
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    },

    async getAccommodation(limit = 5, offset = 0) {
      const longitude = parseFloat(
        parseFloat(localStorage.getItem("longitude") as string).toFixed(6)
      )
      const latitude = parseFloat(parseFloat(localStorage.getItem("latitude") as string).toFixed(6))
      const result = await getAccommodation(longitude, latitude, limit, offset)
      console.log(result)
      if (result.data.code == 200) {
        this.accommodations.accommodationLists = result.data.data.lists
        this.accommodations.accommodationTotal = result.data.data.total
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    },

    async getAccommodationByHot(limit = 5, offset = 0) {
      const result = await getAccommodationByHot(limit, offset)
      console.log(result)
      if (result.data.code == 200) {
        this.accommodationsByHot.accommodationLists = result.data.data.lists
        this.accommodationsByHot.accommodationTotal = result.data.data.total
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    },

    async likeAccommodation(keyword, limit = 5, offset = 0) {
      const result = await likeAccommodation(keyword, limit, offset)
      // console.log(result)
      if (result.data.code == 200) {
        if (result.data.data.total) {
          this.accommodations.accommodationLists = result.data.data.lists
          this.accommodations.accommodationTotal = result.data.data.total
        }
        return result.data.data
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    },

    async saveAccommodation(
      accommodation_id,
      accommodation_uid,
      method: "GET" | "POST" | "DELETE"
    ) {
      console.log(accommodation_id, accommodation_uid, method)

      const result = await saveAccommodation(accommodation_id, accommodation_uid, method)
      // console.log(result)

      if (result.data.code == 200) {
        return result.data
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    },

    async getSaveAccommodation(limit = 5, offset = 0) {
      const result = await getSaveAccommodation(limit, offset)
      // console.log(result)
      if (result.data.code == 200) {
        this.saveAccommodations.accommodationLists = result.data.data.lists
        this.saveAccommodations.accommodationTotal = result.data.data.total
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    },

    async reservation(data: IReservation, method: "POST" | "DELETE") {
      console.log(data, method)

      const result = await reservation(data, method)
      // console.log(result)

      if (result.data.code == 200) {
        return result.data
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    },

    async getReservation(limit = 5, offset = 0) {
      const result = await getReservation(limit, offset)
      console.log(result)
      if (result.data.code == 200) {
        this.reservationAccommodations.accommodationLists = result.data.data.lists
        this.reservationAccommodations.accommodationTotal = result.data.data.total
      } else {
        console.log(result.data.message)
        return Promise.reject(result.data.message)
      }
    }
  },
  getters: {}
})

export default useAccommodationStore
